package com.byzk.p2p.admin.system.controller;

import java.text.ParseException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.byzk.p2p.admin.admin.vo.AdminIndexShow;
import com.byzk.p2p.admin.base.controller.AdminBaseController;
import com.byzk.p2p.admin.base.util.DateUtils;
import com.byzk.p2p.admin.base.util.JsonMsg;
import com.byzk.p2p.admin.borrow.dto.UserTender;
import com.byzk.p2p.admin.borrow.service.TenderService;
import com.byzk.p2p.admin.money.dto.BorrowPayment;
import com.byzk.p2p.admin.money.dto.RechargeRecord;
import com.byzk.p2p.admin.money.service.BorrowPaymentService;
import com.byzk.p2p.admin.money.service.WithdrawService;
import com.byzk.p2p.admin.money.vo.SumWithdrawVo;
import com.byzk.p2p.admin.system.vo.TenderChartVo;
import com.byzk.p2p.admin.user.dto.UserAccount;
import com.byzk.p2p.admin.user.service.UserAccountService;
import com.byzk.p2p.admin.user.service.UserService;

/**
 * 统计图表
 * @author suker.zhou
 *
 */

@Controller
@RequestMapping("/admin/chart")
public class ChartController extends AdminBaseController {

	@Autowired
	private TenderService tenderService;
	@Autowired
	private WithdrawService withdrawService;
	@Autowired
	private UserAccountService accountService;
	@Autowired
	private BorrowPaymentService paymentService;
	@Autowired
	private UserService userService;
	
	/**
	 * 最近10天的投资情况
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping("/tender/last10")
	public String last10tenderMoney() throws Exception{
		Date date=DateUtils.getFirstSecondFewDaysAgo(10);
		List<TenderChartVo> tenderList=tenderService.groupByDate(date);
		super.printJSON(new JsonMsg(-1, "操作成功", tenderList));
		return null;
	}
	
	@RequestMapping("/fund")
	public String fundChart(Model model,@RequestParam(value="dateRange",required=false) String dateRange) throws ParseException{
		Date start=null;Date end=null;
		if(StringUtils.isNotBlank(dateRange)){
			String[] date=dateRange.split("~");
			start=DateUtils.convert(date[0].trim(), DateUtils.YEAR_MOUTH_DAY_FORMAT);
			end=DateUtils.convert(date[1].trim(), DateUtils.YEAR_MOUTH_DAY_FORMAT);
		}
		//成交总金额
		Double totalTenderMoney=tenderService.getAllTenderAmount(start,end);
		//充值总金额
		RechargeRecord record=new RechargeRecord();
		record.setPayMethod(null);
		record.setBegin(start);
		record.setEnd(end);
		Double totalRecharge=accountService.sumTotalRecharge(record);
		//连连充值
		record.setPayMethod("lianlian");
		Double totallianlian=accountService.sumTotalRecharge(record);
		//手动充值
		record.setPayMethod("shoudong");
		Double totalshoudong=accountService.sumTotalRecharge(record);
		
		SumWithdrawVo sumWithdarw=withdrawService.sumByDateDanger(start,end);
		//总注册奖励
		
		//总邀请奖励
		
		//已还总额
		BorrowPayment payment=paymentService.sumPayment(start,end);
		BorrowPayment notPayment=paymentService.sumNotPayment(start,end);
		//下月待还
		BorrowPayment nextMonthPayment=paymentService.nextMonthPayment(DateUtils.getNextMonth());
		//可用金额、冻结金额
		UserAccount account=accountService.sumUserAccount();
		
		AdminIndexShow adminIndexShow = new AdminIndexShow();
		Map<String, Object> pcTerminalMap = new HashMap<String, Object>();
		pcTerminalMap.put("terminal", "PC");
		Map<String, Object> wxTerminalMap = new HashMap<String, Object>();
		wxTerminalMap.put("terminal", "WX");
		Map<String, Object> appTerminalMap = new HashMap<String, Object>();
		appTerminalMap.put("terminal", "APP");
		adminIndexShow.setUserCount(userService.countAll());
		adminIndexShow.setPcUserCount(userService.countAll(pcTerminalMap));
		adminIndexShow.setAppUserCount(userService.countAll(appTerminalMap));
		adminIndexShow.setWxUserCount(userService.countAll(wxTerminalMap));
		adminIndexShow.setIncome(tenderService.getAllRecovedInterest() == null ? 0 : tenderService.getAllRecovedInterest());
		adminIndexShow.setTenderMoney(tenderService.getAllTenderAmount(null,null) == null ? 0 : tenderService.getAllTenderAmount(null,null));
		List<UserTender> userTenders = tenderService.getMonthTender(Integer.valueOf(new DateTime().toString("yyyy")));
		adminIndexShow.setUserTenders(userTenders);
		model.addAttribute("adminIndexShow", adminIndexShow);
		
		model.addAttribute("totalTenderMoney",totalTenderMoney);
		model.addAttribute("totalRecharge",totalRecharge);
		model.addAttribute("totallianlian",totallianlian);
		model.addAttribute("totalshoudong",totalshoudong);
		model.addAttribute("sumWithdarw",sumWithdarw);
		model.addAttribute("payment",payment);
		model.addAttribute("notPayment",notPayment);
		model.addAttribute("nextMonthPayment",nextMonthPayment);
		model.addAttribute("account",account);
		
		model.addAttribute("dateRange",dateRange);
		return ftl(Thread.currentThread().getStackTrace());
	}
}
